package com.jyy.prefabricated.service;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.jyy.prefabricated.entity.PayAccount;
import com.jyy.prefabricated.mapper.PayAccountMapper;
import liquibase.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class WxMerchantsService {
    public static Map<String, WxPayService> merchantsService = new HashMap<String, WxPayService>();
    @Resource
    private PayAccountMapper payAccountDao;
    @Resource
    private WxPayService wxPayService;

    @PostConstruct
    public void init() {
        //系统启动中。。。加载codeMap
        PayAccount payAccount = new PayAccount();
        payAccount.setFlag(0);
        List<PayAccount> payAccountList =  payAccountDao.queryAll(payAccount);
        for (PayAccount payMerchant:payAccountList) {
            WxPayService payService = new WxPayServiceImpl();
            WxPayConfig payConfig = new WxPayConfig();
            payConfig.setAppId(StringUtils.trimToNull(payMerchant.getAppId()));
            payConfig.setMchId(StringUtils.trimToNull(payMerchant.getMchId()));
            payConfig.setMchKey(StringUtils.trimToNull(payMerchant.getKey()));
            payConfig.setSubAppId(StringUtils.trimToNull(""));
            payConfig.setSubMchId(StringUtils.trimToNull(""));
            payConfig.setKeyPath(StringUtils.trimToNull(payMerchant.getKeyPath()));
            payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayService.getConfig().getNotifyUrl()));

            // 可以指定是否使用沙箱环境
            payConfig.setUseSandboxEnv(false);

            payService.setConfig(payConfig);
            merchantsService.put(payMerchant.getTeamCode(),payService);
        }
    }
}
